﻿using Common;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace SFJDManagement
{
    /// <summary>
    /// Win_Main2.xaml 的交互逻辑
    /// </summary>
    public partial class Win_Main2 : Window
    {
        public static Win_Main2 instance;
        Expander menu;
        StackPanel panel;

        public Win_Main2()
        {
            Runtime.Initialize();
            InitializeComponent();
            Title = "司法鉴定办公助手";
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            instance = this;
            Ini();
        }

        private void Ini()
        {
            Label label_User = new Label();
            label_User.SetBinding(ContentProperty, new Binding("UserName") { Source = Runtime.config, Converter = new ConverterUserName() });
            label_User.HorizontalAlignment = HorizontalAlignment.Center;
            label_User.FontSize = 16;
            Layout_Header.Children.Add(label_User);
            //Layout_Header.Children.Add(new Label() { Content = "欢迎！", HorizontalAlignment = HorizontalAlignment.Right, FontSize = 16, Margin = new Thickness(0,0,48,0)});
            menu = new Expander() {
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                ExpandDirection = ExpandDirection.Right,
            };
            SetExpander();
            Layout_Header.Children.Add(menu);
            Layout_Function.NavigationUIVisibility = System.Windows.Navigation.NavigationUIVisibility.Hidden;
            Layout_Function.FontSize = 30;
            Layout_Function.HorizontalAlignment = HorizontalAlignment.Center;
            //Layout_Function.VerticalAlignment = VerticalAlignment.Center;

            //CheckTray();
            ////////////////////////主界面载入////////////////////////
            if (Runtime.xmlTemp.IsEmpty())
            {
                if (Runtime.config.UserLicense == License.User)
                {
                    Layout_Function.Content = new Page_Register();
                }
                else
                {
                    Layout_Function.Content = new Page_Function();
                }
            }
            else
            {
                Layout_Function.Content = new Page_DataErr();
            }
        }

        private void CheckTray()
        {
            bool hasRun = false;
            Process[] pro = Process.GetProcesses();//获取已开启的所有进程
            for (int i = 0; i < pro.Length; i++)//遍历所有查找到的进程
            {
                //判断此进程是否是要查找的进程
                if (pro[i].ProcessName.ToString().ToLower() == "sfjdtray")
                {
                    hasRun = true;
                }
            }
            if (!hasRun)
            {
                Process p = new Process();
                p.StartInfo.FileName = Runtime.programPath + "\\sfjdtray.exe";
                p.Start();
            }
        }

        private void CheckSever()
        {
            ServiceHelper server = new ServiceHelper();
            if (!server.IsServiceStart(Runtime.serviceName))
            {
                bool servering = false;
                try
                {
                    servering = server.StartService(Runtime.serviceName);
                }
                catch (Exception e)
                {
                    Layout_Function.Content = "服务不能启动..\r\n" + e.InnerException.Message;
                    return;
                }
                if (!servering)
                {
                    Layout_Function.Content = "服务不能启动..\r\n";
                }
            }
        }

        private void SetExpander()
        {
            panel = new StackPanel()
            {
                HorizontalAlignment = HorizontalAlignment.Left,
                Orientation = Orientation.Horizontal,
            };
            Button button1 = GetExButton("主菜单");
            button1.Click += Button1_Click;
            Button button2 = GetExButton("设置");
            button2.Click += Button2_Click;
            Button button3 = GetExButton("关于程序");
            button3.Click += Button3_Click;
            panel.Children.Add(button1);
            panel.Children.Add(button2);
            panel.Children.Add(button3);
            menu.Content = panel;
        }

        private void Button3_Click(object sender, RoutedEventArgs e)
        {
            Layout_Function.Content = new Page_Abort();
            menu.IsExpanded = false;
        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            Layout_Function.Content = new Page_ConfigSet();
            menu.IsExpanded = false;
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            Layout_Function.Content = new Page_Function();
            menu.IsExpanded = false;
        }

        private Button GetExButton(string name)
        {
            Button b = new Button()
            {
                Content = name,
                FontSize = 16,
                Margin = new Thickness(5,0,5,0),
            };
            return b;
        }

        private class ConverterUserName : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return "欢迎，" + value + "！";
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
}
